package com.winit.openapi.wh.outbound.builder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.erp.spi.exwarehouse.vo.ExWarehouseItem;
import com.winit.erp.spi.exwarehouse.vo.ExWarehouseProduct;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderItemVo;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderMerchandiseVo;

public class OutboundOrderMerchandiseBuilder {

    public static List<OutboundOrderMerchandiseVo> buildListMerchandise(JSONObject json) {
        List<OutboundOrderMerchandiseVo> list = new ArrayList<OutboundOrderMerchandiseVo>();
        if (json.containsKey("ebayInfoList")) {
            JSONArray array = json.getJSONArray("ebayInfoList");
            for (Object item : array) {
                JSONObject obj = (JSONObject) item;
                OutboundOrderMerchandiseVo vo = new OutboundOrderMerchandiseVo();
                if (obj.containsKey("merchandiseId")) {
                    vo.setId(obj.getLong("merchandiseId"));
                }
                if (obj.containsKey("ebayTransactionId")) {
                    vo.setEbayTransactionId(StringUtils.isNotBlank(obj.getString("ebayTransactionId")) ? obj.getString("ebayTransactionId")
                        .trim() : null);
                }
                if (obj.containsKey("ebayItemId")) {
                    vo.setEbayItemId(StringUtils.isNotBlank(obj.getString("ebayItemId")) ? obj.getString("ebayItemId")
                        .trim() : null);
                }
                if (obj.containsKey("qty")) {
                    vo.setEstimateDispatchQty(obj.getInteger("qty"));
                }
                if (obj.containsKey("replenishmentQty")) {
                    vo.setReplenishmentQty(obj.getInteger("replenishmentQty"));
                }
                list.add(vo);
            }
        }
        return list;
    }

    /**
     * 构建商品列表
     * 
     * @param json
     * @return
     */
    public static List<Map<String, Object>> buildMerchandiseList(List<OutboundOrderMerchandiseVo> list) {
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        for (OutboundOrderMerchandiseVo mvo : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("merchandiseId", mvo.getId());
            String merchandiseCode = mvo.getMerchandiseCode();
            if(merchandiseCode.contains("(return)")){
				map.put("merchandiseCode",
						merchandiseCode.substring(0,merchandiseCode.indexOf("(return)")) + "(return)");
            }else{
            	map.put("merchandiseCode", merchandiseCode);
            }
            map.put("specification", mvo.getSpecification());
            map.put("estimateDispatchQty", mvo.getEstimateDispatchQty());
            map.put("autalQty", mvo.getActualDispatchQty());
            map.put("ebayTransactionId", mvo.getEbayTransactionId());
            map.put("ebayItemId", mvo.getEbayItemId());
            map.put("nameCn", mvo.getNameCn());
            map.put("nameEn", mvo.getNameEn());
            map.put("formerMerchandiseSerno", mvo.getFromMerchandiseSerno());
            map.put("replenishmentQty", mvo.getReplenishmentQty());
            mapList.add(map);
        }
        return mapList;
    }
    
    /**
     * 构建商品列表以及单品
     * 
     * @param json
     * @return
     */
    public static List<Map<String, Object>> buildMerchandiseAndItemList(List<OutboundOrderMerchandiseVo> list) {
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        for (OutboundOrderMerchandiseVo mvo : list) {
            Map<String, Object> map = null;
            List<OutboundOrderItemVo> itemList = mvo.getItemList();
            if(null == itemList || itemList.size() <0 ) continue;
            for (OutboundOrderItemVo item : itemList) {
            	map = new HashMap<String, Object>();
            	String merchandiseCode = mvo.getMerchandiseCode();
                if(merchandiseCode.contains("(return)")){
    				map.put("merchandiseCode",
    						merchandiseCode.substring(0,merchandiseCode.indexOf("(return)")) + "(return)");
                } else {
                	map.put("merchandiseCode", merchandiseCode);
                }
                map.put("specification", mvo.getSpecification());
                map.put("merchandiseSerno", mvo.getMerchandiseSerno());
                map.put("nameCn", mvo.getNameCn());
                map.put("nameEn", mvo.getNameEn());
                map.put("length", mvo.getLength());
                map.put("width",  mvo.getWidth());
                map.put("height", mvo.getHeight());
                map.put("weight", mvo.getWeight());
                map.put("itemSerno", item.getItemSerno());
                mapList.add(map);
			}
        }
        return mapList;
    }
    
    public static List<Map<String, Object>> buildExMerchandiseAndItemList(List<ExWarehouseProduct> list) {
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        for (ExWarehouseProduct ep : list) {
            Map<String, Object> map = null;
            List<ExWarehouseItem> itemList = ep.getItemList();
            if(null == itemList || itemList.size() <0 ) continue;
            for (ExWarehouseItem item : itemList) {
            	map = new HashMap<String, Object>();
            	String merchandiseCode = ep.getMerchandiseCode();
                if(merchandiseCode.contains("(return)")){
    				map.put("merchandiseCode",
    						merchandiseCode.substring(0,merchandiseCode.indexOf("(return)")) + "(return)");
                } else {
                	map.put("merchandiseCode", merchandiseCode);
                }
                map.put("specification", ep.getSpecification());
                map.put("merchandiseSerno", ep.getMerchandiseSerno());
                map.put("nameCn", ep.getNameCn());
                map.put("nameEn", ep.getNameEn());
                map.put("length", ep.getLength());
                map.put("width",  ep.getWidth());
                map.put("height", ep.getHeight());
                map.put("weight", ep.getWeight());
                map.put("itemSerno", item.getItemSerno());
                mapList.add(map);
			}
        }
        return mapList;
    }
}
